package com.secsys.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.secsys.dto.BbsPostDTO;
import com.secsys.dto.BbsReplyDTO;
import com.secsys.model.TbBbsPost;
import com.secsys.model.TbBbsReply;
import com.secsys.model.TbSysUser;
import com.secsys.service.impl.PostServiceImpl;
import com.secsys.service.impl.ReplyServiceImpl;
import com.secsys.service.impl.UserServiceImpl;
import com.secsys.util.DateFormatUtils;

/**
 * @Description 回帖信息Action
 * @author LiusQ
 * @Date 2011-4-9 2:26
 * @version 1.0
 * 
 */
public class BbsReplyAction extends BaseAction<BbsReplyDTO> {
	private BbsReplyDTO replyDTO;
	private ReplyServiceImpl replyService;
	private PostServiceImpl postService;
	private UserServiceImpl userService;
    private String replyAuthorId;
 

	 

	public String getReplyAuthorId() {
		return replyAuthorId;
	}

	public void setReplyAuthorId(String replyAuthorId) {
		this.replyAuthorId = replyAuthorId;
	}

	public void setUserService(UserServiceImpl userService) {
		this.userService = userService;
	}

	public void setPostService(PostServiceImpl postService) {
		this.postService = postService;
	}

	public BbsReplyDTO getReplyDTO() {
		return replyDTO;
	}

	public void setReplyDTO(BbsReplyDTO replyDTO) {
		this.replyDTO = replyDTO;
	}

	public void setReplyService(ReplyServiceImpl replyService) {
		this.replyService = replyService;
	}

	/**
	 * 
	 * @Description 快速回复
	 * @return
	 * @throws Exception
	 */
	public String reply() throws Exception {
		if(replyDTO.getTitle()!=null&&replyDTO.getContent()!=null&&!"".equals(replyDTO.getTitle())&&!"".equals(replyDTO.getContent())){
		TbBbsReply reply = new TbBbsReply();
		reply.setTitle(replyDTO.getTitle());
		reply.setContent(replyDTO.getContent());
		Date date = new Date();
		reply.setReplyTime(DateFormatUtils.convertStringToDateTime(DateFormatUtils.convertDateTimeToString(date)));
		TbSysUser user = userService.findById(replyDTO.getReplyId());
		 
		reply.setTbSysUser(user);
		TbBbsPost post = postService.findById(replyDTO.getId());
		reply.setStatue("审核中");
		reply.setTbBbsPost(post);
		replyService.replyPost(reply);
		post.setReplyNum(replyService.getCount(post));   //统计回复帖子的数目
		post.setLastReplyTime(DateFormatUtils.convertStringToDateTime(DateFormatUtils.convertDateTimeToString(date)));  //设置最后回复帖子的时间
		
		post.setLastReplyNo(replyDTO.getReplyId());  //设置回复人的编号
		postService.saveOrUpdate(post);
		BbsPostDTO posts = postService.queryPostById(replyDTO.getId()); // 获得post
		TbBbsPost bbsPost=new TbBbsPost();
		 bbsPost.setId(replyDTO.getId());
	    List<BbsReplyDTO> replyList=replyService.getReplyById(bbsPost);		// 基本信息
		session.put("post", post);
		System.out.println("回帖成功");
		session.put("replyList", replyList);
		
	 
		return "queryPost";
		}else{
			String  uri = ServletActionContext.getRequest().getHeader("referer");
			  session.put("msg", "对不起，回帖失败,<font color='red'>标题或内容不能为空</font>,现在将跳转到回帖页面,请稍后...");
				 session.put("url", "<a href='"+uri+"'>点击跳返回回帖页面"+"</a>");
				 System.out.println(uri);
				 return "editorReply";
		}
	}
	/**
	 * 
	 * @Description  通过用户查询回复记录 
	 * @return
	 * @throws Exception
	 */
    public String queryReplyByAuthor() throws Exception{
    	       TbSysUser user=new TbSysUser();
    	       user.setId(Integer.parseInt(replyAuthorId));
               List<BbsReplyDTO> replyListByUser=replyService.queryReplyByAuthor(user);
               session.put("replyListByUser", replyListByUser);
               request.put("replyAuthorId", replyAuthorId);
    	return "queryReply";
    }
    
    /**
     * 
     * @Description 屏蔽此贴 
     * @return
     * @throws Exception
     */
    public String delReplyById() throws Exception{
    	 
    	TbBbsReply reply=replyService.findById(Integer.parseInt(replyDTO.getRepId().trim()));
    	reply.setStatue("屏蔽");
    	 replyService.noShowReplyInfo(reply);
    	  
    	 return "queryPostById";
    }
    
	@Override
	public BbsReplyDTO getModel() {
		// TODO Auto-generated method stub
		if (replyDTO == null)
			replyDTO = new BbsReplyDTO();
		return replyDTO;
	}

}
